#include "doorview.h"
#include <QFile>
#include "common.h"
#include <QLabel>
#include <QMovie>
#include "huaqimovie.h"
#include <QApplication>
#include <QMouseEvent>
#include <QHBoxLayout>
#include <QBitmap>

int DoorView::m_id = qRegisterMetaType<DoorView*>();

DoorView::DoorView(QWidget *parent)
    : ViewObject(parent)
    ,m_paramDoorView(NULL)
{  
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_DeleteOnClose);

#ifdef __linux__
    setFocusPolicy(Qt::NoFocus);
#else
    setFocusPolicy(Qt::StrongFocus);
#endif
    if (m_objectParam == NULL)
        m_objectParam = new ParamObject;

    if (m_paramDoorView == NULL)
        m_paramDoorView = new DoorViewParam;
}

DoorView::~DoorView()
{
    if (m_paramDoorView != NULL)
    {
        delete m_paramDoorView;
        m_paramDoorView = NULL;
    }
    if (m_objectParam != NULL)
    {
        delete m_objectParam;
        m_objectParam = NULL;
    }
}

QString DoorView::FindFile(QString prefix, QString name)
{
    if (name == "" || name == "hide")
        return "";

    QString ret;

    ret = myapp::find_file_single(prefix, name);
    if (ret != "")
    {
        AddSuffix(ret);
        return ret;
    }

    QStringList list = name.split("_");
    int count = list.size();

    //找不到门  如:all_door_open,会寻找door_open
    if (count == 3 && name.contains("door"))
    {
        name = list.at(1) + QString("_") + list.at(2);
        ret = myapp::find_file_single(prefix, name);
        if (ret != "")
        {
            AddSuffix(ret);
            return ret;
        }
    }

    return "";
}

void DoorView::LoadDoorParams(QString filename)
{
    if (filename.contains(IntervalFlag))
    {
        m_paramDoorView->m_resOpen      = QString("interval_door_open");
        m_paramDoorView->m_resClose     = QString("interval_door_close");
    }
    else if (filename.contains(ArriveFlag))
    {
        m_paramDoorView->m_resOpen      = QString("arrive_door_open");
        m_paramDoorView->m_resClose     = QString("arrive_door_close");
    }
    else if (filename.contains(AllFlag))
    {
        m_paramDoorView->m_resOpen      = QString("all_door_open");
        m_paramDoorView->m_resClose     = QString("all_door_close");
    }
    m_paramDoorView->m_resIsolation = QString("door_isolation");
    m_paramDoorView->m_resFault     = QString("door_fault");
    m_paramDoorView->m_resObstacle  = QString("door_obstacle");
}

void DoorView::ShowObject()
{
    if (m_paramDoorView == NULL || m_objectParam == NULL) return;

    int door_status = DOOR_OPEN;

    LoadDoorParams(mFilename);

    if (mProtocolData)
        door_status = mProtocolData->door_status;

    if (door_status == DOOR_EMPTY) return;

    if (door_status == DOOR_OPEN)
        url = m_paramDoorView->m_resOpen;
    else if (door_status == DOOR_CLOSE)
        url = m_paramDoorView->m_resClose;
    else if (door_status == DOOR_ISOLATION)
        url = m_paramDoorView->m_resIsolation;
    else if (door_status == DOOR_FAULT)
        url = m_paramDoorView->m_resFault;
    else if (door_status == DOOR_OBSTACLE)
        url = m_paramDoorView->m_resObstacle;
    else
        url = m_paramDoorView->m_resOpen;

    ShowUrl();
}

void DoorView::GetAttribute(std::string& attribute)
{
    std::string jsonStr;
    Json::Value base;
    Json::StreamWriterBuilder writerBuilder;
    std::ostringstream os;

    base["zindex"] = QString::number(m_objectParam->GetIndex()).toStdString();
    base["stid"] = QString::number(m_objectParam->GetStId()).toStdString();

    base["x"] = QString::number(m_objectParam->GetX()).toStdString();
    base["y"] = QString::number(m_objectParam->GetY()).toStdString();
    base["w"] = QString::number(m_objectParam->GetWidth()).toStdString();
    base["h"] = QString::number(m_objectParam->GetHeight()).toStdString();
    base["name"] = m_objectParam->GetName().toStdString();

    std::unique_ptr<Json::StreamWriter> jsonWriter(writerBuilder.newStreamWriter());
    jsonWriter->write(base, &os);
    jsonStr = os.str();

    attribute = jsonStr;
}

void DoorView::SetAttribute(std::string jsonStr)
{
    bool res;
    JSONCPP_STRING errs;
    Json::Value root;
    Json::CharReaderBuilder readerBuilder;
    std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
    res = jsonReader->parse(jsonStr.c_str(), jsonStr.c_str()+jsonStr.length(), &root, &errs);

    if (!res || !errs.empty())
    {
         LOG_QDEBUG<<__FUNCTION__<<"json parse faild"<<QString::fromStdString(errs);
    }

    if (CheckJsonValueType(root, "zindex", Json::stringValue))
    {
        m_objectParam->SetIndex(QString::fromStdString(root["zindex"].asString()).toInt());
    }

    if (CheckJsonValueType(root, "stid", Json::stringValue))
    {
        m_objectParam->SetStId(QString::fromStdString(root["stid"].asString()).toInt());
    }

    if (CheckJsonValueType(root, "x", Json::stringValue))
    {
        m_objectParam->SetX(QString::fromStdString(root["x"].asString()).toInt());
    }

    if (CheckJsonValueType(root, "y", Json::stringValue))
    {
        m_objectParam->SetY(QString::fromStdString(root["y"].asString()).toInt());
    }

    if (CheckJsonValueType(root, "w", Json::stringValue))
    {
        m_objectParam->SetWidth(QString::fromStdString(root["w"].asString()).toInt());
    }

    if (CheckJsonValueType(root, "h", Json::stringValue))
    {
        m_objectParam->SetHeight(QString::fromStdString(root["h"].asString()).toInt());
    }

    if (CheckJsonValueType(root, "name", Json::stringValue))
    {
        m_objectParam->SetName(QString::fromStdString(root["name"].asString()));
    }
}

void DoorView::ParseXML(QDomElement* xmlDomEle)
{
    if (m_paramDoorView == NULL)
        m_paramDoorView = new DoorViewParam;

    m_objectParam->SetIndex((xmlDomEle->attribute("zindex","0")).toInt());
    m_objectParam->SetStId((xmlDomEle->attribute("stid","0")).toInt());
    m_objectParam->SetX((xmlDomEle->attribute("x","0")).toInt());
    m_objectParam->SetY((xmlDomEle->attribute("y","0")).toInt());
    m_objectParam->SetWidth((xmlDomEle->attribute("w","0")).toInt());
    m_objectParam->SetHeight((xmlDomEle->attribute("h","0")).toInt());
    m_objectParam->SetName((xmlDomEle->attribute("name","")));
}

void DoorView::PacketXML(QDomElement* dom)
{
    QDomDocument document;
    *dom = document.createElement("item");
    dom->setAttribute("type", m_objectParam->GetObjectName());

    dom->setAttribute("zindex", QString::number(m_objectParam->GetIndex()));
    dom->setAttribute("stid", QString::number(m_objectParam->GetStId()));
    dom->setAttribute("x", QString::number(m_objectParam->GetX()));
    dom->setAttribute("y", QString::number(m_objectParam->GetY()));
    dom->setAttribute("w", QString::number(m_objectParam->GetWidth()));
    dom->setAttribute("h", QString::number(m_objectParam->GetHeight()));
    dom->setAttribute("name", m_objectParam->GetName());
}
